package com.pig4cloud.pig.choicesetup.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import com.pig4cloud.pig.choicesetup.entity.ChoiceSettingEntity;
import com.pig4cloud.pig.choicesetup.service.ChoiceSettingService;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import com.pig4cloud.pig.common.security.annotation.HasPermission;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 选项配置表
 *
 * @author pig
 * @date 2025-04-12 14:44:53
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/choicesetup" )
@Tag(description = "choicesetup" , name = "选项配置表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ChoiceSettingController {

    private final  ChoiceSettingService choiceSettingService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param choiceSetting 选项配置表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @HasPermission("choicesetup_choicesetup_view")
    public R getChoiceSettingPage(@ParameterObject Page page, @ParameterObject ChoiceSettingEntity choiceSetting) {
        LambdaQueryWrapper<ChoiceSettingEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(choiceSettingService.page(page, wrapper));
    }


    /**
     * 通过条件查询选项配置表
     * @param choiceSetting 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
    @GetMapping("/details" )
    @HasPermission("choicesetup_choicesetup_view")
    public R getDetails(@ParameterObject ChoiceSettingEntity choiceSetting) {
        return R.ok(choiceSettingService.list(Wrappers.query(choiceSetting)));
    }

    /**
     * 新增选项配置表
     * @param choiceSetting 选项配置表
     * @return R
     */
    @Operation(summary = "新增选项配置表" , description = "新增选项配置表" )
    @SysLog("新增选项配置表" )
    @PostMapping
    @HasPermission("choicesetup_choicesetup_add")
    public R save(@RequestBody ChoiceSettingEntity choiceSetting) {
        return R.ok(choiceSettingService.save(choiceSetting));
    }

    /**
     * 修改选项配置表
     * @param choiceSetting 选项配置表
     * @return R
     */
    @Operation(summary = "修改选项配置表" , description = "修改选项配置表" )
    @SysLog("修改选项配置表" )
    @PutMapping
    @HasPermission("choicesetup_choicesetup_edit")
    public R updateById(@RequestBody ChoiceSettingEntity choiceSetting) {
        return R.ok(choiceSettingService.updateById(choiceSetting));
    }

    /**
     * 通过id删除选项配置表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除选项配置表" , description = "通过id删除选项配置表" )
    @SysLog("通过id删除选项配置表" )
    @DeleteMapping
    @HasPermission("choicesetup_choicesetup_del")
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(choiceSettingService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param choiceSetting 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @HasPermission("choicesetup_choicesetup_export")
    public List<ChoiceSettingEntity> exportExcel(ChoiceSettingEntity choiceSetting,Integer[] ids) {
        return choiceSettingService.list(Wrappers.lambdaQuery(choiceSetting).in(ArrayUtil.isNotEmpty(ids), ChoiceSettingEntity::getId, ids));
    }

    /**
     * 导入excel 表
     * @param choiceSettingList 对象实体列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @HasPermission("choicesetup_choicesetup_export")
    public R importExcel(@RequestExcel List<ChoiceSettingEntity> choiceSettingList, BindingResult bindingResult) {
        return R.ok(choiceSettingService.saveBatch(choiceSettingList));
    }
}
